<?php
class Pages_layoutAction extends IntesysAction {
	//当进入该页面时，跳转到Index-index，并触发布局状态
	public function index(){
		R('Intesys/Index/index',['start']);
		//$index->index();
	}
	//保存页面布局
	public function savePage(){
		if($_GET['title']){
			//先查询数据库有没有当前用户的页面，有就将更改保存，没有就创建一条新的数据
			$id=$this->dao->where(['title'=>$_GET['title'],'member_id'=>$this->userInfo['id']])->getField('id');
			
			if($id){
				$result=$this->dao->where(['id'=>$id])->save(['source'=>serialize($_GET['data']),'layout'=>$_GET['layout'],'page_name'=>$_GET['title'],'datetime'=>time()]);
			}else{
				$data=$this->dao->where('title="'.$_GET['title'].'" and site_id='.__SITEID__)->find();
				$data['source']=serialize($_GET['data']);
				$data['layout']=$_GET['layout'];
				$data['title']=$_GET['title'];
				$data['datetime']=time();
				$data['member_id']=$this->userInfo['id'];
				unset($data['id']);
				$result=$this->dao->add($data);
			}
			echo(json_encode(['statu'=>$result,'info'=>$this->dao->getError(),'reback'=>$_GET]));
		}
	}
	//获取页面布局
	public function getPage(){
		if($_GET['title']){
			echo(json_encode($this->dao->formatLayout($_GET['title'])));
		}
	}
	public function insert(){
		$_POST['datetime']=time();
		$this->dao=M('Modules_layout');
		parent::insert();
	}
	public function add(){
		$this->display();
	}
	//显示模块
	public function indexModule(){
		if((D('Member')->where('id = '.$this->memberInfo['id'])->getfield('isadministrator',false))=='1'){
			$memberstatu=1;//boss
		}else{
			$memberstatu=0;
		}
		if($memberstatu == 1){//老板
			$this->totalstaff=D('Member')->where('site_id ='.__SITEID__)->count();
			//部门奖扣分
			$this->indexpos='超级管理员';
			$time=time();
			$monthstart=strtotime(date('Y',$time).'-'.date('m',$time)."-1");
			if(date('m',$time)==12){
				$monthend=strtotime(date('Y-m-31 23:59:59',$time));
			}else{
				$monthend=strtotime(date('Y',$time).'-'.(date('m',$time)+1).'-1');
			}
			$post['i.time']=$get['i.time']=array('between',array($monthstart,$monthend));
			$post['i.type_id']=$get['i.type_id']=D('Integral_type')->where("typename = 'Binte' and site_id =".__SITEID__)->getfield('id',false);
			$post['i.site_id']=$get['i.site_id']=__SITEID__;
			$get['i.changenum']=array('gt','0');
			$post['i.changenum']=array('lt','0');

			$getsql=D('Integral_log')->alias('i')->join('right join __MEMBER__ m on m.id = i.giver_id')->join('right join __MEMBER_POSITION__ p on p.member_id =i.giver_id')->where($get)->group('i.giver_id')->field('p.name as pname,sum(i.changenum) as sum')->select();
			$postsql=D('Integral_log')->alias('i')->join('right join __MEMBER__ m on m.id = i.giver_id')->join('right join __MEMBER_POSITION__ p on p.member_id =i.giver_id')->where($post)->group('i.giver_id')->field('p.name as pname,sum(i.changenum) as dsum')->select();
			foreach($getsql as $k =>$v){
				$addarr[$k]['name']=$v['pname'];
				$addarr[$k]['sum']=intval($v['sum']);
				$addarr[$k]['dsum']=0;
			}
			foreach($postsql as $ke =>$ve){
				foreach($addarr as $k =>$v){
					if(array_key_exists('dsum',$addarr[$k]))$addarr[$k]['dsum']=abs(intval($ve['dsum']));
					if (!array_key_exists('sum',$addarr[$k]))$addarr[$k]['sum']=0;
				}
			}
			$this->assign('addarr',$addarr);
			//积分排行榜
			$limit["t.dateline"]=array('between',array($monthstart,$monthend));
			$limit['t.integraltypeid']=D('Integral_type')->where("site_id = ".__SITEID__." and typename = 'Binte' ")->getfield('id',false);
			$member=D('Member')->where('site_id ='.__SITEID__)->getfield('id',true);
			$limit['t.member_id']=array('in',$member);   //department(s)
			$all=D('Integralranking')->alias('t')->join('__MEMBER__ as m on m.id = t.member_id')->where($limit)->group('t.member_id')->order('sum(num) desc')->field("t.*,sum(num) as sum,m.name as name,m.nickname as nickname,m.memberpositionid")->limit(5)->select();
			foreach($all as $key =>$value){
				$mdep=array_values(array_filter(explode(',',$value['memberpositionid'])));//数组洗牌
				foreach($mdep as $k =>$v){
					$pname=D('Member_position')->where('id = '.$v)->getfield('name',false);
					$all[$key]['pname'].="【".$pname."】";//使用$all[$key]而不是$value
				}
				$all[$key]['k']=$key+1;
			}
			$this->assign('all',$all);
		}else{
			//部门
			$memberposition=D('Member')->where('site_id ='.__SITEID__.' and id='.$this->memberInfo['id'])->getfield('memberpositionname');
			$mdep=array_values(array_filter(explode(',',$memberposition)));//数组洗牌
			$indexpos='';
			foreach($mdep as $k =>$v){
				$indexpos.=" ".$v." ";//使用$all[$key]而不是$value
			}
			$this->assign('indexpos',$indexpos);
		}
		$this->assign('memberstatu',$memberstatu);
		$this->display();
	}
	public function total(){
		//积分
		if($this->_param('show')=='staff'){
			
			
			if(isset($_GET['member_id'])){
				$member_id = intval($_GET['member_id']);
			}else{
				$member_id = $this->memberInfo['id'];
			}
			$tmid = $member_id;
		$memberposition=D('Member')->where('site_id ='.__SITEID__.' and id='.$member_id)->getfield('memberpositionid');
		$mdep=array_values(array_filter(explode(',',$memberposition)));
		$time=time();
		if($this->_param('time')=='y'){
			$start=strtotime(date('Y',$time)."-1-1");
			$end=strtotime((date('Y',$time)+1)."-1-1");
		}elseif($this->_param('time')=='m'){
			$start=strtotime(date('Y',$time)."-".date('m',$time)."-1");
			if(date('m',$time)==12){
				$end=strtotime(date('Y-m-31 23:59:59',$time));
			}else{
				$end=strtotime(date('Y',$time).'-'.(date('m',$time)+1).'-1');
			}
		}elseif($this->_param('time')=='w'){
			$date=new DateTime();
			$date->modify('this week');
			$start=strtotime($date->format('Y-m-d'));
			$date->modify('this week +6 days');
			$end=strtotime($date->format('Y-m-d'))+23*59*59;
		}else{
			$start=strtotime(date('Y-m-d 00:00:00',$time));
			$end=strtotime(date('Y-m-d 23:59:59',$time));
		}
		$member_id='';
		if($this->_param('target')!=null){
		$member_id=' and member_id = '.$this->_param('target').'  ';
			}
		
		
		$map["t.dateline"]=array('between',array($start,$end));
		$map['t.integraltypeid']=D('Integral_type')->where("site_id = ".__SITEID__." and typename = 'Binte' ")->getfield('id',false);
		$member=D('Member')->where('site_id ='.__SITEID__)->getfield('id',true);
		$map['t.member_id']=array('in',$member);
		$all=D('Integralranking')->alias('t')->join('__MEMBER__ as m on m.id = t.member_id')->where($map)->group('t.member_id')->order('sum(num) desc')->field("t.*,sum(t.num) as sum")->select();
		foreach($all as $key =>$value){
			$mp=array_values(array_filter(explode(',',$value['memberpositionid'])));//数组洗牌
			foreach($mp as $k =>$v){
				$pname=D('Member_position')->where('id = '.$v)->getfield('name',false);
				$all[$key]['pname'].="【".$pname."】";
			}
		if($value['member_id']==$tmid){ 
				$this->assign('totalgrade',$value['sum']);
				$this->assign('allid',$key+1);
			}
		}
		$member=D('Member')->where('site_id ='.__SITEID__." and memberpositionid like '%,".$mdep[0].",%'")->getfield('id',true);
		$map['t.member_id']=array('in',$member);
		$depart=D('Integralranking')->alias('t')->where($map)->group('t.member_id')->order('sum(num) desc')->select();
		foreach($depart as $k =>$v){
			if($v['member_id']==$tmid){
				$this->assign('depid',$k+1);
				
			}
		
		}
		$template='1';
	  
		}else if($this->_param('show')=='position'||$this->_param('show')=='site'){
		
		$lwn=new Lwn_gradeAction();	
		$total=$lwn->grade();	
    
	    if($template==null){
			$template='2';
		}
		$this->assign('totalgrade',$total['positive']);
		
		$this->assign('allid',$total['minus']);
		
		$this->assign('depid',$total['count']);
		
		
			}
		  $this->assign('lwn_template',$template);		
		$this->display();
	}
	//测试新方法
	public function test(){
		import ( '@.ORG.RBACM' );
		dump($this->memberInfo['id']);
		dump(RBACM::getAccessList($this->memberInfo['id']));exit;
		//D('Formcategory')->copy(322,505,['site_id'=>322]);
		//dump(D('Mission')->daily());
		
		//$infoDB=D('Info');
		//dump($infoDB->sendMsg([['content'=>'Hello world!','mode'=>'HXMsg,WXMsg,WXTempMsg','tomemberid'=>2177,'type'=>'remind']]));
		/*
		$redis = new Redis();
	    $redis->connect('127.0.0.1', 6379);
	    $redis->auth('pjx.19930603');
	    echo "Connection to server sucessfully";
		 //查看服务是否运行
	    echo "Server is running: " . $redis->ping();*/
	}
}